
using Abp.Domain.Repositories;
using Abp.Extensions;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.Customized;
using SOEI.Solution.DomainEntities.Parameters.DomainService;
using SOEI.Solution.Integratedes.Organization;
using SOEI.Solution.Integratedes.Organization.DomainService;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace SOEI.Solution.DomainEntities.Organization.DomainService
{
    /// <summary>
    /// 领域服务层一个模块的核心业务逻辑
    ///</summary>
    public class EconomyOrganiManager : DomainServiceBase, IEconomyOrganiManager
    {

        private readonly IRepository<EconomyOrgani, long> _economyorganiRepository;

        // 综合系统 机构领域服务
        private readonly IOEconomyOrganiManager _oeconomyOrganiManager;
        // 综合系统 机构仓储
        private readonly IRepository<OEconomyOrgani, string> _oeconomyorganiRepository;

        /// <summary>
        /// 【Parameter】参数的领域服务
        /// </summary>
        private readonly IParameterManager _parameterManager;

        /// <summary>
        /// 【EconomyOrgani】的构造方法
        /// 通过构造函数注册服务到依赖注入容器中
        ///</summary>
        public EconomyOrganiManager(
            IRepository<EconomyOrgani, long> economyorganiRepository,
            IRepository<OEconomyOrgani, string> oeconomyorganiRepository,
            IOEconomyOrganiManager oeconomyOrganiManager,
            IParameterManager parameterManager
        )
        {
            _economyorganiRepository = economyorganiRepository;
            _oeconomyorganiRepository = oeconomyorganiRepository;
            _oeconomyOrganiManager = oeconomyOrganiManager;
            _parameterManager = parameterManager;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        /// 返回列表查询用l
        /// </summary>
        /// <returns></returns>
        public IQueryable<EconomyOrgani> QueryEntityListAsNoTracking()
        {

            var query = _economyorganiRepository.GetAll().AsNoTracking()
                        .Select(x => new EconomyOrgani
                        {
                            Id = x.Id,
                            JGBH = x.JGBH,
                            DZ = x.DZ,
                            ZGBM = x.ZGBM,
                            ZCZJ = x.ZCZJ,
                            FRXM = x.FRXM,
                            FRDH = x.FRDH,
                            LXRXM = x.LXRXM,
                            LXRDH = x.LXRDH,
                            YWLXDH = x.YWLXDH,
                            YZBH = x.YZBH,
                            RYZS = x.RYZS,
                            JJRS = x.JJRS,
                            YYZZH = x.YYZZH,
                            SWDJZH = x.SWDJZH,
                            FRDMZH = x.FRDMZH,
                            FZJGS = x.FZJGS,
                            YWFW = x.YWFW,
                            ZJLY = x.ZJLY,
                            GDZJ = x.GDZJ,
                            LDZJ = x.LDZJ,
                            ZJZJ = x.ZJZJ,
                            ZYMJ = x.ZYMJ,
                            ZLMJ = x.ZLMJ,
                            QTMJ = x.QTMJ,
                            MJHJ = x.MJHJ,
                            JGLXDM = x.JGLXDM,
                            SJDW = x.SJDW,
                            FZRXM = x.FZRXM,
                            FZRDH = x.FZRDH,
                            JJXZDM = x.JJXZDM,
                            LRSJ = x.LRSJ,
                            JGZT = x.JGZT,
                            GLYBH = x.GLYBH,
                            BGMJ = x.BGMJ,
                            ZCDZ = x.ZCDZ,
                            ZCDZYB = x.ZCDZYB,
                            BGCSDM = x.BGCSDM,
                            SGRS = x.SGRS,
                            KZJQDMMHTBH = x.KZJQDMMHTBH,
                            JJHTJYBH = x.JJHTJYBH,
                            JYGPXXBH = x.JYGPXXBH,
                            JJRXLZS = x.JJRXLZS,
                            XGSJ = x.XGSJ,
                            EMAIL = x.EMAIL,
                            QQ = x.QQ,
                            NJYQSJ = x.NJYQSJ,
                            SFFZJG = x.SFFZJG,
                            ZGS = x.ZGS,
                            YZGSGX = x.YZGSGX,
                            PYJC = x.PYJC,
                            JGZP = x.JGZP,
                            KWQ = x.KWQ,
                            JGMC = x.JGMC,
                            SFYQ = x.SFYQ,
                            WQKTSJ = x.WQKTSJ,
                            ZGSJGBH = x.ZGSJGBH,
                            FRZJLX = x.FRZJLX,
                            FRZJHM = x.FRZJHM,
                            JGQC = x.JGQC,
                            SFGTGSH = x.SFGTGSH,
                        });
            return query;
        }

        /// <summary>
        /// 【EconomyOrgani】返回表达式数的实体信息即IQueryable类型
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<EconomyOrgani> Query()
        {
            return _economyorganiRepository.GetAll();
        }
        /// <summary>
        /// 【EconomyOrgani】返回即IQueryable类型的实体，不包含EF Core跟踪标记
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<EconomyOrgani> QueryAsNoTracking()
        {
            return _economyorganiRepository.GetAll().AsNoTracking();
        }
        /// <summary>
        /// 【EconomyOrgani】根据Id查询实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>实体</returns>
        public async Task<EconomyOrgani> FindByIdAsync(long id)
        {
            var entity = await _economyorganiRepository.GetAsync(id);
            return entity;
        }
        /// <summary>
        /// 【EconomyOrgani】检查实体是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns>bool</returns>
        public async Task<bool> IsExistAsync(long id)
        {
            var result = await _economyorganiRepository.GetAll().AnyAsync(a => a.Id == id);
            return result;
        }
        /// <summary>
        /// 【EconomyOrgani】创建实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<EconomyOrgani> CreateAsync(EconomyOrgani entity)
        {
            entity.Id = await _economyorganiRepository.InsertAndGetIdAsync(entity);
            return entity;
        }
        /// <summary>
        /// 【EconomyOrgani】更新实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task UpdateAsync(EconomyOrgani entity)
        {
            await _economyorganiRepository.UpdateAsync(entity);
        }
        /// <summary>
        /// 【EconomyOrgani】删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(long id)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _economyorganiRepository.DeleteAsync(id);
        }
        /// <summary>
        /// 【EconomyOrgani】批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task BatchDelete(List<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _economyorganiRepository.DeleteAsync(a => input.Contains(a.Id));
        }
        #endregion


        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展领域服务接口*/

        /// <summary>
        /// 通过关联查询获取列表查询结果
        /// </summary>
        /// <returns></returns>
        public IQueryable<EconomyOrgani> QueryUnionEntityListAsNoTracking()
        {
            // linqforSql
            // 这里目前必须要把类型查出来然后在联合查询
            var ListEntity = this._parameterManager.QueryApprovalStatus().ToList();
            var query = this.QueryEntityListAsNoTracking();
            // 联合查询
            var result = from x in query
                         join b in ListEntity on x.JGZT equals b.Id into dc
                         from dci in dc.DefaultIfEmpty()

                         select new EconomyOrgani
                         {
                             Id = x.Id,
                             JGBH = x.JGBH,
                             DZ = x.DZ,
                             ZGBM = x.ZGBM,
                             ZCZJ = x.ZCZJ,
                             FRXM = x.FRXM,
                             FRDH = x.FRDH,
                             LXRXM = x.LXRXM,
                             LXRDH = x.LXRDH,
                             YWLXDH = x.YWLXDH,
                             YZBH = x.YZBH,
                             RYZS = x.RYZS,
                             JJRS = x.JJRS,
                             YYZZH = x.YYZZH,
                             SWDJZH = x.SWDJZH,
                             FRDMZH = x.FRDMZH,
                             FZJGS = x.FZJGS,
                             YWFW = x.YWFW,
                             ZJLY = x.ZJLY,
                             GDZJ = x.GDZJ,
                             LDZJ = x.LDZJ,
                             ZJZJ = x.ZJZJ,
                             ZYMJ = x.ZYMJ,
                             ZLMJ = x.ZLMJ,
                             QTMJ = x.QTMJ,
                             MJHJ = x.MJHJ,
                             JGLXDM = x.JGLXDM,
                             SJDW = x.SJDW,
                             FZRXM = x.FZRXM,
                             FZRDH = x.FZRDH,
                             JJXZDM = x.JJXZDM,
                             LRSJ = x.LRSJ,
                             JGZT = x.JGZT,
                             GLYBH = x.GLYBH,
                             BGMJ = x.BGMJ,
                             ZCDZ = x.ZCDZ,
                             ZCDZYB = x.ZCDZYB,
                             BGCSDM = x.BGCSDM,
                             SGRS = x.SGRS,
                             KZJQDMMHTBH = x.KZJQDMMHTBH,
                             JJHTJYBH = x.JJHTJYBH,
                             JYGPXXBH = x.JYGPXXBH,
                             JJRXLZS = x.JJRXLZS,
                             XGSJ = x.XGSJ,
                             EMAIL = x.EMAIL,
                             QQ = x.QQ,
                             NJYQSJ = x.NJYQSJ,
                             SFFZJG = x.SFFZJG,
                             ZGS = x.ZGS,
                             YZGSGX = x.YZGSGX,
                             PYJC = x.PYJC,
                             JGZP = x.JGZP,
                             KWQ = x.KWQ,
                             JGMC = x.JGMC,
                             SFYQ = x.SFYQ,
                             WQKTSJ = x.WQKTSJ,
                             ZGSJGBH = x.ZGSJGBH,
                             FRZJLX = x.FRZJLX,
                             FRZJHM = x.FRZJHM,
                             JGQC = x.JGQC,
                             SFGTGSH = x.SFGTGSH,
                         };

            return result;
        }

        /// <summary>
        /// 【EconomyOrgani】根据Id查询实体信息(重构)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>实体</returns>
        public async Task<EconomyOrgani> GetEntityByIdAsync(long id)
        {
            var query = this.QueryUnionEntityListAsNoTracking().AsNoTracking();

            var entity = await query.FirstOrDefaultAsync(x => x.Id == id);
            return entity;
        }

        /// <summary>
        /// 【EconomyOrgani】提交审核 （提交审核后才会产生机构编号）
        /// </summary>
        /// <param name="id">要提交的机构Id</param>
        /// <returns></returns>
        public async Task SubmitApproval(long id)
        {
            //   本系统构经济机构
            var entity = await this.FindByIdAsync(id);
            // 1.  设置为 待审核
            entity.JGZT = ((int)EApprovalStatus.WaitReview).ToString();

            // 2. 转换为综合系统实体
            var oEntity = this.ConvertToOEconomyOrgani(entity);

            //新增还是更新
            if (oEntity.Id.IsNullOrEmpty())
            {
                // 第一次申请 同步到zhxt 中去
                oEntity.Id = _oeconomyOrganiManager.GetNewSequenceCode();
                await this._oeconomyOrganiManager.CreateAsync(oEntity);
            }
            else
            {
                oEntity.BGCSDM = string.Empty;
                // 存在就是更新
                await this._oeconomyOrganiManager.UpdateAsync(oEntity);
            }

            entity.JGBH = oEntity.Id;
            // 4.  保存
            await this.UpdateAsync(entity);
        }

        #endregion











    }
}
